package es.udc.emarketapp.emarket.model.admin.dao;

import es.udc.emarketapp.emarket.model.admin.vo.AdminVO;
import es.udc.fbellas.j2ee.util.exceptions.InstanceNotFoundException;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.sql.GeneralOperations;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public abstract class AbstractSQLAdminDAO implements SQLAdminDAO {

    public boolean exists(Connection connection, String idAdmin)
            throws InternalErrorException {

        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {

            // Create "preparedStatement".
            String queryString = "SELECT id_admin FROM administrador "
                    + "WHERE id_admin = ?";
            preparedStatement = connection.prepareStatement(queryString);

            // Fill "preparedStatement".
            preparedStatement.setString(1, idAdmin);

            // Execute query.
            resultSet = preparedStatement.executeQuery();

            return resultSet.next();

        } catch (SQLException e) {
            throw new InternalErrorException(e);
        } finally {
            GeneralOperations.closeResultSet(resultSet);
            GeneralOperations.closeStatement(preparedStatement);
        }
    }//exists

    public boolean exists(Connection connection, String idAdmin, String pass)
            throws InternalErrorException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public AdminVO findById(Connection connection, String idAdmin)
            throws InternalErrorException, InstanceNotFoundException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {

            // Create "preparedStatement".
            String queryString = "SELECT pass FROM administrador "
                    + "WHERE id_admin = ?";
            preparedStatement = connection.prepareStatement(queryString);

            // Fill "preparedStatement".
            preparedStatement.setString(1, idAdmin);

            // Execute query.
            resultSet = preparedStatement.executeQuery();

            if(!resultSet.next()){
                throw new InstanceNotFoundException(idAdmin,
                        AdminVO.class.getName());
            }

            //Get results

            String pass = resultSet.getString(1);

            return  new AdminVO(idAdmin, pass);

        } catch (SQLException e) {
            throw new InternalErrorException(e);
        } finally {
            GeneralOperations.closeResultSet(resultSet);
            GeneralOperations.closeStatement(preparedStatement);
        }
    }//findById

    public AdminVO findById(Connection connection, String idAdmin, String pass)
            throws InternalErrorException, InstanceNotFoundException {
        throw new UnsupportedOperationException("Not supported yet.");

    }
}
